//本进程处理工具类页面相关内容

const applyMysql = require('./tools/applyMysql');


const os = require('os');
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const koaStatic = require('koa-static');
const indexRouter = require('./http/routes/tools');
let Router = require('koa-router');
const {resolve} = require('path')
const fs = require('fs');
const ini = require('ini');
const Cors = require('koa-cors');
async function init() {
    let mysqlInstance = new applyMysql();

    let file = await giveFileName();
    let ini = await readIni(file);
    

    global.instance = {
        dbHandler : mysqlInstance,

        ini:ini,
        fileName:file,
    };

}

async function main() {
    await init();

    start();

}


async function giveFileName() {
    let type = os.platform();
    console.log('type = ',type);
    let fileName;
    if(type == "darwin")
    {
        fileName = "/Users/hideyoshi/Desktop/codes/my-web/BackEnd/run.ini";

    }
    else if(type == "win32")
    {
        fileName = "F:/my-web/BackEnd/run.ini";

    }
    else
    {
        fileName = "./run.ini"

    }
    return fileName;
}


async function readIni(fileName) {
    let file = fs.readFileSync(fileName);
    var Info = ini.parse(file.toString());
    return Info;
}

function start() {
    const app = new Koa();
    app.use(bodyParser());
    app.use(Cors());
    var router = new Router();
    router.use('/tools',indexRouter);
    app.use(router.routes());
    app.use(router.allowedMethods());

    let port = instance.ini.Proxy.Tools;
    app.listen(port, () => {
        console.log("http服务器已启动，127.0.0.1:",port);
    })
}


main();